package com.workcell.workdata.im.component.validator;

import com.workcell.workdata.im.annotation.validator.MessageMapFormat;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.model.message.ChatMap;
import com.workcell.workdata.im.model.request.MessageRequest;
import com.workcell.workdata.im.util.JSON;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MessageMapValidator implements ConstraintValidator<MessageMapFormat, MessageRequest> {

    @Override
    public boolean isValid(MessageRequest momentRequest, ConstraintValidatorContext context) {

        if (momentRequest.getFormat() != MessageFormat.MAP){
            return true;
        }

        ChatMap map = JSON.parseNullable(momentRequest.getContent(), ChatMap.class);

        return map != null
                && map.getAddress() != null
                && map.getLongitude() != 0
                && map.getLatitude() != 0;
    }
}
